package com.lqh.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lqh.mapper.StudentMapper;
import com.lqh.pojo.PageResult;
import com.lqh.pojo.Student;
import com.lqh.pojo.StudentQueryParam;
import com.lqh.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult<Student> pageQuery(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        List<Student> list = studentMapper.pageQuery(studentQueryParam);
        Page<Student> page =  (Page<Student>) list;
        return new PageResult<>(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    @Override
    public void addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.addStudent(student);
    }

    @Override
    public Student getInfo(Integer id) {
        return studentMapper.getInfo(id);
    }

    @Override
    public void updateStudent(Student student) {
        studentMapper.deleteByIds(Arrays.asList(student.getId()));
        studentMapper.addStudent(student);
    }

    @Override
    public void updateViolation(Integer id, Integer score) {
        Student info = studentMapper.getInfo(id);
        Short violationCount = info.getViolationCount();
        Short violationScore = info.getViolationScore();
        studentMapper.updateViolation(id,violationCount+1,violationScore+score);
    }
}
